package com.buildings.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.buildings.R;
import com.buildings.adapter.ImgFileListAdapter;
import com.buildings.bean.FileTraversal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.buildings.adapter.ImgFileListAdapter;
import com.buildings.util.Util;

public class ImgFileListActivity extends Activity implements
        OnItemClickListener {
    private TextView tv_quxiao;
    private int count;
    ListView listView;
    ImgFileListAdapter listAdapter;
    List<FileTraversal> locallist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.commactivity_imgfilelist);

        count = this.getIntent().getIntExtra("count", 0);
        tv_quxiao = (TextView) this.findViewById(R.id.tv_quxiao);
        listView = (ListView) findViewById(R.id.listView1);

        // 获取本地图片文件集合
        locallist = Util.LocalImgFileList(this);

        // 将文件集合转换为需要的map数据
        List<HashMap<String, String>> listdata = new ArrayList<HashMap<String, String>>();
        if (locallist != null) {
            for (int i = 0; i < locallist.size(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("filecount", locallist.get(i).filecontent.size()
                        + getString(R.string.commactivity_activity_img_file_unit));
                Log.d("FOLDER PATCH", "----------->"
                        + locallist.get(i).filecontent.get(0));
                map.put("imgpath",
                        locallist.get(i).filecontent.get(0) == null ? null
                                : (locallist.get(i).filecontent.get(0)));
                map.put("filename", locallist.get(i).filename);
                listdata.add(map);
            }
        }

        // 数据设置到列表上展示
        listAdapter = new ImgFileListAdapter(this, listdata);
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(this);

        tv_quxiao.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Intent intent = new Intent(this, ImgsActivity.class);
        intent.putExtra("count", count);
        Bundle bundle = new Bundle();
        bundle.putParcelable("data", locallist.get(arg2));
        intent.putExtras(bundle);
        // startActivityForResult(intent, ProductPhotoActivity.Load_Image);
        startActivityForResult(intent, 1000);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000 && resultCode == RESULT_OK) {
            ArrayList<String> files = new ArrayList<String>();
            files = data.getStringArrayListExtra("files");
            Intent intent = new Intent();
            intent.putStringArrayListExtra("files", files);
            this.setResult(RESULT_OK, intent);
            this.finish();
        }
    }

    // @Override
    // protected void saveTemp(Bundle bundle) {
    // // TODO Auto-generated method stub
    //
    // }
    //
    // @Override
    // protected void resumeTemp(Bundle bundle) {
    // // TODO Auto-generated method stub
    //
    // }
    //
    // @Override
    // protected void initTitle(View v) {
    // // TODO Auto-generated method stub
    //
    // }

}
